package com.fun.demo.annotation;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class ImgUtil {

    public static void main(String[] args) throws IOException {
        tranImageSize();
    }

    //个人大头照
    public static byte[] tranImageSize() throws IOException {
       /* File file = new File("C:/Users/dell/Desktop/11-16");
        String[] files = file.list();
        for (String fileName : files){
            System.out.println(fileName + "");
            byte[] photo = getBytes("C:/Users/dell/Desktop/11-16/" + fileName);
            tranImageSize(photo, 240, 320);
            System.out.println("\n\n");
        }*/
        //88001299.jpg  88001308.jpg
        byte[] photo = getBytes("C:/Users/dell/Desktop/11-16/88001299.jpg");
        byte[] data = tranImageSize(photo, 240, 320);
        byteToFile(data, "C:/Users/dell/Desktop/11-16/88001299_new.jpg");
        return null;
    }


    /**
     * 将byte写入文件中
     *
     * @param data
     * @param filePath
     * @throws IOException
     */
    public static void byteToFile(byte[] data, String filePath) throws IOException {
        OutputStream os = null;
        try {
            File file = new File(filePath);
            File fileParent = file.getParentFile();
            if (!file.exists()) {
                if (!fileParent.exists()) {
                    fileParent.mkdirs();
                }
                file.createNewFile();
            }
            os = new FileOutputStream(file);
            os.write(data);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null)
                os.close();
        }
    }

    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (IOException ex) {
           ex.printStackTrace();
        }
        return buffer;
    }

    public static BufferedImage toBufferedImage(byte[] photo) throws MalformedURLException {
//        if (image instanceof BufferedImage) {
//            return (BufferedImage) image;
//        }
        // This code ensures that all the pixels in the image are loaded
        URL url = new URL("C:/Users/dell/Desktop/11-16/88001299.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        Image image = imageIcon.getImage();
        int type = BufferedImage.TYPE_INT_RGB;
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        System.out.println("image.getWidth(null) : " + width);
        System.out.println("image.getHeight(null) : " + height);
        if(width == -1 || height == -1){
            width = 240;
            height = 320;
        }
        BufferedImage bimage = new BufferedImage(width, height, type);
        // Copy image to buffered image
        Graphics g = bimage.createGraphics();
        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return bimage;
    }

    //将照片变小再显示要省资源
    public static byte[] tranImageSize(byte[] photo, int width, int height) {
        if (width <= 10 || height <= 10) {
            width = 300;
            height = 400;
        }
        ByteArrayOutputStream out = null;
        try {
            //Image imgTemp = ImageIO.read(new ByteArrayInputStream(photo)); 部分图片可能变红
           // Image imgTemp = Toolkit.getDefaultToolkit().createImage(photo);
            BufferedImage img = toBufferedImage(photo);
            if ((img.getWidth() > width) && (img.getHeight() > height)) {
                if (img.getWidth() * 1.0 / width > img.getHeight() * 1.0 / height)
                    height = img.getHeight() * width / img.getWidth();
                else
                    width = img.getWidth() * height / img.getHeight(); //高度有限
            } else if (img.getWidth() > width) {
                height = img.getHeight() * width / img.getWidth();  //宽度优先
            } else if (img.getHeight() > height) {
                width = img.getWidth() * height / img.getHeight();
            } else {
                width = img.getWidth();
                height = img.getHeight();
            }

            BufferedImage tag = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
            out = new ByteArrayOutputStream();
            ImageIO.write(tag, "JPG", out);
            return out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}
